﻿// -----------------------------------------------------------------------
// <copyright file="NewPlanarModel.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;



namespace ObjectLibrary
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PlanarModel : MyModel
    {
        //(-1, -1, 0) A (0, 0)
        //(-1, 1, 0)  B (0, 1)
        //(1, 1, 0)   C (1, 1)
        //(1, -1, 0)  D (1, 0)
        private const float textureScale = 0.01f;
        protected VertexPositionTexture[] _vertices;
        protected Texture2D _texture;
        protected Vector4 _filterColor;
        protected float _light;
        private int _layer;

        public float Light
        {
            get { return _light; }
            set { _light = value; }
        }

        public Vector4 FilterColor
        {
            get { return _filterColor; }
            set { _filterColor = value; }
        }

        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }
        protected Effect _effect;

        public Effect Effect
        {
            get { return _effect; }
            set { _effect = value; }
        }

        protected Vector2 _size;

        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }
        public PlanarModel(ContentManager content, Texture2D texture, float scale, Vector3 pos, float rotationY)
        {
            _effect = content.Load<Effect>(@"Resources/Effects/Effect");


            _texture = texture;

            Init(0,scale, pos, rotationY);
        }

        public PlanarModel(ContentManager content, string strTexture, float scale, Vector3 pos, float rotationY)
        {
            _effect = content.Load<Effect>(@"Resources/Effects/Effect");

            if (strTexture != null)
            {
                _texture = content.Load<Texture2D>(strTexture);
            }
            Init(0,scale, pos, rotationY);
        }

        public PlanarModel(ContentManager content, string strTexture, float scale, Vector3 pos, Matrix rotation)
        {
            _effect = content.Load<Effect>(@"Resources/Effects/Effect");

            if (strTexture != null)
            {
                _texture = content.Load<Texture2D>(strTexture);
            }
            _rotation = rotation;
            Init(0, scale, pos, 0);
        }

        public PlanarModel(ContentManager content, Texture2D texture, int layer, float scale, Vector3 pos, float rotationY)
        {
            _effect = content.Load<Effect>(@"Resources/Effects/Effect");

            _texture = texture;
            Init(0,scale, pos, rotationY);
        }

        public void setTexture(Texture2D texture)
        {
            _texture = texture;
            Init(_layer,_scale,_position, _rotationY);
        }
        private void Init(int layer,float scale, Vector3 pos, float rotationY)
        {
            _scale = scale;
            _rotationY = rotationY;
            

            float widthTexture = _texture.Width;
            float heightTexture = _texture.Height;

            _transformMatrix = Matrix.CreateRotationY(rotationY)* _rotation * Matrix.CreateScale(_scale) * Matrix.CreateTranslation(_position);

            _vertices = new VertexPositionTexture[6];

          /*  Vector3 A = Vector3.Normalize(new Vector3(-widthTexture, heightTexture, 0));
            Vector3 B = Vector3.Normalize(new Vector3(-widthTexture, -heightTexture, 0));
            Vector3 C = Vector3.Normalize(new Vector3(widthTexture, -heightTexture, 0));
            Vector3 D = Vector3.Normalize(new Vector3(widthTexture, heightTexture, 0));*/
            Vector3 A = (new Vector3(-widthTexture, heightTexture, layer)) * textureScale;
            Vector3 B = (new Vector3(-widthTexture, -heightTexture, layer)) * textureScale;
            Vector3 C = (new Vector3(widthTexture, -heightTexture, layer)) * textureScale;
            Vector3 D = (new Vector3(widthTexture, heightTexture, layer)) * textureScale;

            _vertices[0] = new VertexPositionTexture(A, new Vector2(0, 0));// A
            _vertices[1] = new VertexPositionTexture(C, new Vector2(1, 1));  // C
            _vertices[2] = new VertexPositionTexture(B, new Vector2(0, 1)); // B

            _vertices[3] = new VertexPositionTexture(A, new Vector2(0, 0)); // A
            _vertices[4] = new VertexPositionTexture(D, new Vector2(1, 0)); // D
            _vertices[5] = new VertexPositionTexture(C, new Vector2(1, 1)); // C


            _size.X = D.X * 2 * scale;
            _size.Y = D.Y * 2 * scale;
            _position = pos ;
            _filterColor = Vector4.One;
            _layer = layer;
            _light = 1;
        }



        public override void Draw(GameTime gameTime, GraphicsDevice graphicsDevice, Effect effect, MyCamera camera)
        {
            _effect.CurrentTechnique = _effect.Techniques["Technique2"];
            _effect.Parameters["World"].SetValue(TransformMatrix);
            _effect.Parameters["View"].SetValue(camera.View);
            _effect.Parameters["Projection"].SetValue(camera.Projection);
            _effect.Parameters["Texture1"].SetValue(_texture);
            _effect.Parameters["FilterColor"].SetValue(_filterColor);
            _effect.Parameters["Light"].SetValue(_light);
            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, _vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
            }
        }

        public bool isClicked(Vector3 contactPoint)
        {

            Matrix untransform = Matrix.CreateRotationY(-_rotationY);
            Vector3 pos = Vector3.Transform(Position, untransform);
            Vector3 pointer = Vector3.Transform(contactPoint, untransform);
            if (pointer.X > pos.X - Size.X / 2
                        && pointer.X < pos.X + Size.X / 2
                        && pointer.Y > pos.Y - Size.Y / 2
                        && pointer.Y < pos.Y + Size.Y / 2)
                return true;
            return false;
        }
        public float ClickPosition(Vector3 contactPoint)
        {

            Matrix untransform = Matrix.CreateRotationY(-_rotationY);
            Vector3 pos = Vector3.Transform(Position, untransform);
            Vector3 pointer = Vector3.Transform(contactPoint, untransform);
            if (pointer.X > pos.X - Size.X / 2
                        && pointer.X < pos.X + Size.X / 2
                        && pointer.Y > pos.Y - Size.Y / 2
                        && pointer.Y < pos.Y + Size.Y / 2)
                return (pointer.X - (pos.X - Size.X/2)) / _size.X;
            return -1;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _transformMatrix = Matrix.CreateRotationY(_rotationY) * _rotation * Matrix.CreateScale(_scale) * Matrix.CreateTranslation(_position);
        }
    }
}
